﻿#pragma once
#include"HashTable.h"

namespace dan
{
	template<class K,class V>
	class unordered_map
	{
		struct MapKeyOfT
		{
			const K& operator()(const pair<K, V>& kv)
			{
				return kv.first;
			}
		};
	public:
		typedef typename hash_bucket::HashTable<pair<const K, V>, K, MapKeyOfT>::Iterator iterator;
		typedef typename hash_bucket::HashTable<pair<const K, V>, K, MapKeyOfT>::ConstIterator const_iterator;
	public:
		pair<iterator,bool> insert(const pair<K, V>& kv)
		{
			return _ht.Insert(kv);
		}

		iterator begin()
		{
			return _ht.Begin();
		}

		iterator end()
		{
			return _ht.End();
		}

		const_iterator begin()const
		{
			return _ht.Begin();
		}

		const_iterator end()const
		{
			return _ht.End();
		}

		iterator find(const pair<const K, V>& kv)
		{
			return _ht.Find(kv.first);
		}

		bool erase(const pair<const K, V>& kv)
		{
			return Erase(kv.first);
		}

		V& operator[](const K& k)
		{
			pair<iterator, bool>ret = _ht.Insert({k,V()});
			return ret.first->second;
		}

	private:
		hash_bucket::HashTable<pair<const K, V>, K,MapKeyOfT> _ht;
	};

	void test_map()
	{
		unordered_map<string, string> m;
		m.insert({ "sort", "" });
		m.insert({ "left", "" });

		m.insert({ "right", "ұ" });
		// 
		m["insert"];
		for (auto& e : m)
		{
			// e.first += 'x';
			e.second += 'x';

			cout << e.first << ":" << e.second << endl;
		}
		cout << endl;

		m["insert"] = "";
		for (auto& e : m)
		{
			// e.first += 'x';
			//e.second += 'x';

			cout << e.first << ":" << e.second << endl;
		}
		cout << endl;

		
		m["string"] = "ַ";
		for (auto& e : m)
		{
			// e.first += 'x';
			//e.second += 'x';

			cout << e.first << ":" << e.second << endl;
		}
		cout << endl;

		string arr[] = { "ƻ", "", "ƻ", "", "ƻ", "ƻ", "","ƻ", "㽶", "ƻ", "㽶" };
		unordered_map<string, int> countMap;
		for (auto& str : arr)
		{
			countMap[str]++;
		}

		for (auto& e : countMap)
		{
			cout << e.first << ":" << e.second << endl;
		}
		cout << endl;
	}
}